package com.CityHunter.Handler;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import com.CityHunter.Activity.GroupView;

import android.content.SharedPreferences;
import android.widget.Toast;

public class Http_Handler {

	
	public ArrayList<HashMap<String, Object>> getGorupList(String email) {
		
		ArrayList<HashMap<String, Object>> groups = new ArrayList<HashMap<String, Object>>();
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.add(new BasicNameValuePair("email", email));
		try {

			 HttpClient httpclient = new DefaultHttpClient();
				HttpPost httppost = new HttpPost(
						"http://tamama.slyrc.org/FYP/FYP/getGROUP.php");
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				String result = EntityUtils.toString(entity);
				JSONArray json = new JSONArray(result);
				

			for (int i = 0; i < json.length(); i++) {
		
				HashMap<String, Object> group = new HashMap<String, Object>();

				group.put("name",  json.getJSONObject(i).getString("groupTitle"));
				group.put("status", "online");
				groups.add(group);

			}
		} catch (Exception e) {
			
		}
		
	

		return groups;
	}
	public ArrayList<HashMap<String, Object>> getGFriendsByGroup(String groupTitle,String name) {
		 
		ArrayList<HashMap<String, Object>> friends = new ArrayList<HashMap<String, Object>>();
		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.add(new BasicNameValuePair("grouptitle", groupTitle));
		nameValuePairs.add(new BasicNameValuePair("name", name));
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
					"http://tamama.slyrc.org/FYP/FYP/getGROUP_MEMBER.php");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			String result = EntityUtils.toString(entity);
			JSONArray json = new JSONArray(result);
		

			for (int i = 0; i < json.length(); i++) {
				HashMap<String, Object> friend = new HashMap<String, Object>();

				friend.put("name",  json.getJSONObject(i).getString("name"));
				friend.put("status", "online");
				friends.add(friend);
				

			}
			
		} catch (Exception e) {
		
		}
		
		return friends;
	}
}
